package com.kanshu.ksgb.fastread.doudou.module.login.thirdlogin

import android.app.Activity
import android.text.TextUtils
import android.util.Log
import com.alipay.sdk.app.AuthTask
import com.kanshu.ksgb.fastread.doudou.R
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.BindInfo
import io.reactivex.schedulers.Schedulers
import org.greenrobot.eventbus.EventBus


class AlipayLogin(private val activity:Activity){
 //       private var alipayClient: AlipayClient
  //  private var privateKey:String = activity.getString(R.string.alipay_app_private_key)
    private var appId:String = activity.getString(R.string.alipay_app_id_withdraw)
    private val PID = "2088131624151657"


    init {
//        alipayClient = DefaultAlipayClient("https://openapi.alipay.com/gateway.do",appId,privateKey)
    }

   private fun doBindAlipayUser(sign: String,targetId:String):BindInfo{
       val result = BindInfo(false)
       result.type = BindInfo.TYPE_ALIPAY
       var authResult = auth(sign,targetId)

       if (!TextUtils.equals(authResult.resultStatus, "9000") || !TextUtils.equals(authResult.resultCode, "200")) {
           Log.i("test","authCode == null")
           return result
       }
       result.result = true
       result.authorization_id = authResult.authCode
       result.type_user_id = authResult.alipayOpenId
       return result
   }



    fun bindAliPayUser(sign: String,targetId: String) {
        val worker = Schedulers.io().createWorker()
        worker.schedule {
           val result = doBindAlipayUser(sign,targetId)
            EventBus.getDefault().post(result)
        }
    }

    private fun auth(sign:String,target_id:String):AuthResult {
        val rsa2 = true
        val authInfoMap = OrderInfoUtil2_0.buildAuthInfoMap(PID,appId,target_id, rsa2)
        val info = OrderInfoUtil2_0.buildOrderParam(authInfoMap)
        val authInfo = "$info&$sign"
        val auth =  AuthTask(activity)
        val response = auth.authV2(authInfo,false)
        response.keys?.forEach{
            Log.i("test" ,"$it = ${response.get(it)}")
        }

        val authResult = AuthResult(response, true)
        val resultStatus = authResult.resultStatus

        // 判断resultStatus 为“9000”且result_code
        // 为“200”则代表授权成功，具体状态码代表含义可参考授权接口文档
        //if (TextUtils.equals(resultStatus, "9000") && TextUtils.equals(authResult.resultCode, "200")) {
            // 获取alipay_open_id，调支付时作为参数extern_token 的value
            // 传入，则支付账户为该授权账户
         //   authCode = authResult.authCode
        //} else {
            // 其他状态值则为授权失败
         //   Toast.makeText(activity, "授权失败" + String.format("authCode:%s", authResult.authCode), Toast.LENGTH_SHORT).show()

        //}
        return authResult

    }


/*   private fun login():AlipayUserInfoAuthResponse{
        val request = AlipayUserInfoAuthRequest ()
        request.bizContent = "{" +
                "      \"scopes\":[" +
                "        \"auth_base\"" +
                "      ]," +
                "\"state\":\"init\"," +
                "\"is_mobile\":\"true\"" +
                "  }"
      return  alipayClient.sdkExecute(request)
    }

    private fun getToken(authCode:String):AlipayOpenAuthTokenAppResponse{
        val requestSystemToken = AlipayOpenAuthTokenAppRequest()
        requestSystemToken.bizContent = "{" +
                "    \"grant_type\":\"authorization_code\"," +
                "    \"code\":\"$authCode\"" +
                "  }"
        requestSystemToken.notifyUrl = ""
        return alipayClient.execute(requestSystemToken) as AlipayOpenAuthTokenAppResponse

    }

    private fun getUserInfo(accessToken:String):AlipayUserInfoShareResponse{
        val request = AlipayUserInfoShareRequest()
       return alipayClient.execute(request, accessToken) as AlipayUserInfoShareResponse
    }*/
}